/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.desktop.kungkhie.importer.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JFileChooser;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//com.kungkhies.desktop.kungkhie.importer.ui//KungkhieImporterUI//EN",
autostore = false)
@TopComponent.Description(preferredID = "KungkhieImporterUITopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.kungkhies.desktop.kungkhie.importer.ui.KungkhieImporterUITopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_KungkhieImporterUIAction",
preferredID = "KungkhieImporterUITopComponent")
public final class KungkhieImporterUITopComponent extends TopComponent
{
    private JFileChooser jFileChooser1;
    private int returnVal;
    
    public KungkhieImporterUITopComponent()
    {
        initComponents();
        setName(NbBundle.getMessage(KungkhieImporterUITopComponent.class, "CTL_KungkhieImporterUITopComponent"));
        setToolTipText(NbBundle.getMessage(KungkhieImporterUITopComponent.class, "HINT_KungkhieImporterUITopComponent"));
        
        jFileChooser1 = new JFileChooser();
        kungkhieFileTextArea.setEditable(false);

        ActionListener actionListener = new ActionListener()
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                jFileChooser1 = (JFileChooser) actionEvent.getSource();
                String command = actionEvent.getActionCommand();
                if (command.equals(JFileChooser.APPROVE_SELECTION))
                {
                    File selectedFile = jFileChooser1.getSelectedFile();
                    System.out.println(selectedFile.getParent());
                    selectedFileTextField.setText(selectedFile.getName());
                    kungkhieFileTextArea.setText("");
                    readFileIntoTextArea(selectedFile);
                }
                else if (command.equals(JFileChooser.CANCEL_SELECTION))
                {
                    System.out.println(JFileChooser.CANCEL_SELECTION);
                }
            }
        };

        jFileChooser1.addActionListener(actionListener);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        kungkhieImporterTitleLabel = new javax.swing.JLabel();
        filenameLabel = new javax.swing.JLabel();
        selectedFileTextField = new javax.swing.JTextField();
        fileChooserButton = new javax.swing.JButton();
        importButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        errorMessageLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        kungkhieFileTextArea = new javax.swing.JTextArea();
        fileContentsLabel = new javax.swing.JLabel();

        kungkhieImporterTitleLabel.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(kungkhieImporterTitleLabel, org.openide.util.NbBundle.getMessage(KungkhieImporterUITopComponent.class, "KungkhieImporterUITopComponent.kungkhieImporterTitleLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(filenameLabel, org.openide.util.NbBundle.getMessage(KungkhieImporterUITopComponent.class, "KungkhieImporterUITopComponent.filenameLabel.text")); // NOI18N

        selectedFileTextField.setText(org.openide.util.NbBundle.getMessage(KungkhieImporterUITopComponent.class, "KungkhieImporterUITopComponent.selectedFileTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(fileChooserButton, org.openide.util.NbBundle.getMessage(KungkhieImporterUITopComponent.class, "KungkhieImporterUITopComponent.fileChooserButton.text")); // NOI18N
        fileChooserButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fileChooserButtonMouseClicked(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(importButton, org.openide.util.NbBundle.getMessage(KungkhieImporterUITopComponent.class, "KungkhieImporterUITopComponent.importButton.text")); // NOI18N
        importButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                importButtonMouseClicked(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(KungkhieImporterUITopComponent.class, "KungkhieImporterUITopComponent.cancelButton.text")); // NOI18N
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });

        errorMessageLabel.setForeground(new java.awt.Color(255, 0, 0));
        org.openide.awt.Mnemonics.setLocalizedText(errorMessageLabel, org.openide.util.NbBundle.getMessage(KungkhieImporterUITopComponent.class, "KungkhieImporterUITopComponent.errorMessageLabel.text")); // NOI18N

        kungkhieFileTextArea.setColumns(20);
        kungkhieFileTextArea.setRows(5);
        jScrollPane1.setViewportView(kungkhieFileTextArea);

        org.openide.awt.Mnemonics.setLocalizedText(fileContentsLabel, org.openide.util.NbBundle.getMessage(KungkhieImporterUITopComponent.class, "KungkhieImporterUITopComponent.fileContentsLabel.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(filenameLabel)
                            .addComponent(kungkhieImporterTitleLabel)
                            .addComponent(selectedFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 521, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
                            .addComponent(fileContentsLabel)
                            .addComponent(fileChooserButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(errorMessageLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(importButton)
                                .addGap(18, 18, 18)
                                .addComponent(cancelButton)))))
                .addGap(217, 217, 217))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(kungkhieImporterTitleLabel)
                .addGap(62, 62, 62)
                .addComponent(filenameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(selectedFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(fileChooserButton)
                .addGap(48, 48, 48)
                .addComponent(fileContentsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(importButton)
                    .addComponent(cancelButton))
                .addGap(60, 60, 60)
                .addComponent(errorMessageLabel)
                .addGap(102, 102, 102))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void fileChooserButtonMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_fileChooserButtonMouseClicked
    {//GEN-HEADEREND:event_fileChooserButtonMouseClicked
        returnVal = jFileChooser1.showOpenDialog(this);
    }//GEN-LAST:event_fileChooserButtonMouseClicked

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_cancelButtonMouseClicked
    {//GEN-HEADEREND:event_cancelButtonMouseClicked
        clearAndClose();
    }//GEN-LAST:event_cancelButtonMouseClicked

    private void importButtonMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_importButtonMouseClicked
    {//GEN-HEADEREND:event_importButtonMouseClicked
        errorMessageLabel.setText("Not implemented yet!");
    }//GEN-LAST:event_importButtonMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel errorMessageLabel;
    private javax.swing.JButton fileChooserButton;
    private javax.swing.JLabel fileContentsLabel;
    private javax.swing.JLabel filenameLabel;
    private javax.swing.JButton importButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea kungkhieFileTextArea;
    private javax.swing.JLabel kungkhieImporterTitleLabel;
    private javax.swing.JTextField selectedFileTextField;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p)
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p)
    {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
    
    public void readFileIntoTextArea(File selectedFile)
    {
        FileObject fileObject = FileUtil.toFileObject(FileUtil.normalizeFile(selectedFile));

        try
        {
            InputStream is = fileObject.getInputStream();

            InputStreamReader reader = new InputStreamReader(is);

            BufferedReader buf = new BufferedReader(reader);

            String line;
            try
            {
                while ((line = buf.readLine()) != null)
                {
                    kungkhieFileTextArea.append(line + "\n");
                }
            }
            catch (IOException ex)
            {
                Exceptions.printStackTrace(ex);
            }

        }
        catch (FileNotFoundException ex)
        {
            Exceptions.printStackTrace(ex);
        }
    }
    
    public void clearAndClose()
    {
        kungkhieFileTextArea.setText("");
        selectedFileTextField.setText("");
        this.close();   
    }
}
